package com.example.hotelrear.controller;


import com.example.hotelrear.constant.JwtClaimsConstant;
import com.example.hotelrear.entity.ResponseResult;
import com.example.hotelrear.entity.User;
import com.example.hotelrear.properties.JwtProperties;
import com.example.hotelrear.service.UserService;
import com.example.hotelrear.util.JwtUtil;
import com.example.hotelrear.vo.UserLoginVO;
import org.apache.ibatis.annotations.Param;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.Map;


/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author FengXiao
 * @since 2023-08-28
 */
@RestController
@RequestMapping("/user")
@CrossOrigin("*")
public class UserController {
    @Resource
    private UserService userService;
    @Resource
    private JwtProperties jwtProperties;

    //用户登录
    @PostMapping("/login")
    public ResponseResult login(@Param("username") String username, @Param("password") String password) {
        //验证码
//        if (!code.equals("1111")) {
//            return ResponseResult.fail("验证码错误");
//        }
        User user = new User();
        user.setUsername(username);
        user.setPassword(password);
        User selectuser = userService.login(user);
        if (selectuser != null) {
            //登录成功后，生成jwt令牌
            Map<String, Object> claims = new HashMap<>();
            claims.put(JwtClaimsConstant.USER_ID, user.getId());
            String token = JwtUtil.createJWT(
                    jwtProperties.getUserSecretKey(),
                    jwtProperties.getUserTtl(),
                    claims);
            System.out.println(token);

            UserLoginVO userLoginVO = UserLoginVO.builder()
                    .id(user.getId())
                    .username(user.getUsername())
                    .token(token)
                    .build();

            return ResponseResult.ok(userLoginVO);
        }


        return ResponseResult.fail("登录失败");


    }

    //检测用户名是否重复
    @GetMapping("/regexUsername")
    public ResponseResult regexUsername(@Param("username") String username) {
        User user = userService.regexUsername(username);
        if (user != null) {
            return ResponseResult.fail("用户名重复");
        }
        return ResponseResult.ok("恭喜，该账户还没有被注册");

    }

    //检测手机号是否重复
    @GetMapping("/regexPhone")
    public ResponseResult regexPhone(@Param("phone") String phone) {
        User user = userService.regexPhone(phone);
        if (user != null) {
            return ResponseResult.fail("手机号重复");
        }
        return ResponseResult.ok("该手机号可以使用");

    }

    //注册
    @PostMapping("/regist")
    public ResponseResult regist(@RequestBody User user) {
        System.out.println(user);
        boolean rows = userService.regist(user);
        if (rows) {
            return ResponseResult.ok(rows);
        }
        return ResponseResult.fail("注册失败");
    }

    //修改个人信息
    @PostMapping("/updateUser")
    public ResponseResult updateUser(@RequestBody User user) {
        System.out.println(user);
        boolean rows = userService.updateUser(user);
        if (rows) {
            return ResponseResult.ok(rows);
        }
        return ResponseResult.fail("修改失败");
    }


}
